package com.hqyj.order.controller;

import com.hqyj.order.model.OrderInfo;
import com.hqyj.sale.model.SaleInfo;
import frame.order.controller.BaseController;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author cz
 * 2021/9/6 14:46
 */
@Controller
@RequestMapping("/orderController")
public class OrderController extends BaseController {

    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
        //1.接收数据;
        //3.调用业务;
        List<OrderInfo> orderInfo_dbList = orderInfoService.queryAllOrderInfo();
        model.addAttribute("orderInfo_dbList", orderInfo_dbList);
        //2.跳转页面;
        return "view/order/orderInfoList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String idStr) {
        //1.接收数据;
        int id = Integer.parseInt(idStr);
        //3.调用业务;
        OrderInfo orderInfo_db = orderInfoService.queryOrderInfoById(id);
        model.addAttribute("orderInfo_db", orderInfo_db);
        //2.跳转页面;
        return "view/order/updateOrderInfo";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String idStr,OrderInfo orderInfo){
        //1.接收数据;
        int id = Integer.parseInt(idStr);
        orderInfo.setId(id);

        //3.调用业务;
        int n = orderInfoService.updateOrderInfoByOrderInfo(orderInfo);

        //重新查询返回跳转
        List<OrderInfo> orderInfo_dbList = orderInfoService.queryAllOrderInfo();
        model.addAttribute("orderInfo_dbList", orderInfo_dbList);
        //2.跳转页面;
        return "view/order/orderInfoList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,OrderInfo orderInfo){
        //1.接收数据;

        //3.调用业务;
        int n = orderInfoService.addOrderInfoByOrderInfo(orderInfo);
        int m = productPlanService.addProductPlanByOrderInfo(orderInfo.getProductName(),orderInfo.getOrderQuantity());

        //重新查询返回跳转
        List<OrderInfo> orderInfo_dbList = orderInfoService.queryAllOrderInfo();
        model.addAttribute("orderInfo_dbList", orderInfo_dbList);
        //2.跳转页面;
        return "view/order/orderInfoList";
    }

    @RequestMapping("/send.do")
    public String send(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, String idStr){
        //1.接收数据;
        int id = Integer.parseInt(idStr);

        //3.调用业务;
        int m = orderInfoService.updateOrderInfoByOrderState(id);
        OrderInfo orderInfo = orderInfoService.queryOrderInfoById(id);
        int n = orderInfoService.addSaleInfoByOrderInfo(orderInfo);

        //重新查询返回跳转
        List<OrderInfo> orderInfo_dbList = orderInfoService.queryAllOrderInfo();
        model.addAttribute("orderInfo_dbList", orderInfo_dbList);
        //2.跳转页面;
        return "view/order/orderInfoList";
    }

    @RequestMapping("/listSend.do")
    public String listSend(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {
        //1.接收数据;
        //3.调用业务;
        List<OrderInfo> orderInfo_dbList = orderInfoService.queryAllOrderInfoBySended();
        model.addAttribute("orderInfo_dbList", orderInfo_dbList);
        //2.跳转页面;
        return "view/order/orderInfoListSended";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String idStr){
        //1.接收数据;
        int id = Integer.parseInt(idStr);
        //3.调用业务;
        int n = orderInfoService.deleteOrderInfoByid(id);

        //重新查询返回跳转
        List<OrderInfo> orderInfo_dbList = orderInfoService.queryAllOrderInfoBySended();
        model.addAttribute("orderInfo_dbList", orderInfo_dbList);
        //2.跳转页面;
        return "view/order/orderInfoListSended";
    }
}
